package com.wayhome.srpingbootmybatis.utils.cob;

import java.io.IOException;
import java.security.GeneralSecurityException;

public class CertP7 {

    public static  String sign(String data) throws Exception {
        String pathPfx = CertP7.class.getClassLoader().getResource("cob/95566SW060000009.pfx").getPath();
        PKCS7Tool pkcs = PKCS7Tool.getSigner(pathPfx, "1234", "1234");
        return pkcs.sign(data.getBytes());
    }

    public static void verify(String signature, String data) throws GeneralSecurityException, IOException {
        // 获取验签证书路径
        String pathCer = CertP7.class.getClassLoader().getResource("cob/verify.cer").getPath();
        // 创建PKCS7验签工具
        PKCS7Tool pkcs = PKCS7Tool.getVerifier(pathCer);

        pkcs.verify(signature, data.getBytes(), null);
    }

    public static void main(String[] args) {
        try {
            String sign = sign("ll");
            System.out.println(sign);
            verify(sign, "ll");
        }catch (Exception e) {
            e.printStackTrace();
        }

    }
}
